import LXCOM from '../../../common/common.js'
import tezhengbuzhuosvc from '../../../service/tezhengbuzhuosvc.js';
import absorbItemToConatinerSvc from '../../../service/absorbItemToConatinerSvc.js';
import absorbsizesvc from '../../../service/absorbsizesvc.js';
import globalConst from '../../../common/globalConst.js';
var global = globalConst.GlobalConst;

var LMosDragTool = {

}

LMosDragTool.action = function (dataManger, mosEvnt, tPoint, preLMosDownPt) {
    var nModifyInfo = 0;
    var tValidPoint = new LXCOM.LGXPoint();
    tValidPoint.clone(tPoint);

    var tOrignalPoint = new LXCOM.LGXPoint();
    tOrignalPoint.clone(tPoint);

    var offsetPt = new LXCOM.LGXPoint();
    offsetPt.x = tPoint.x - preLMosDownPt.x;
    offsetPt.y = tPoint.y - preLMosDownPt.y;
    var nMosMovDis = offsetPt.x * offsetPt.x + offsetPt.y * offsetPt.y;
    if (nMosMovDis > 0) {
        if (!dataManger.getDragFlag()){
            let canDrag = this.startDragGraph(dataManger,tPoint);
            if (canDrag){
                dataManger.setDragFlag(true);
                let transGap = nMosMovDis*dataManger.getScaling();
                if (transGap < global.SelectGap){
                    //初始拖拽，如果距离太短，则不处理。
                    return 0;
                }
            }
            else{
                return 0;
            }
        }
        let pCurentGraph = dataManger.getCurentGraph();
        if (pCurentGraph){
            dataManger.recordRepaintScope(pCurentGraph);
        }
        let absorbInfo = absorbItemToConatinerSvc.AbsorbItemToContainerSvc.absorb(dataManger, tPoint, offsetPt);
        if (!absorbInfo){
            absorbInfo = tezhengbuzhuosvc.TezhengBuzhuoSvc.absorbMouse2OtherGraphFeature(dataManger, tPoint, offsetPt,true);
        }
        
        absorbsizesvc.AbsorbSizeSvc.absorbToSetSameSize(dataManger, tPoint, offsetPt); 
        if (absorbInfo) {
            if (absorbInfo.xabsorb) {
                offsetPt.x = absorbInfo.offset.x;
                tPoint.x = preLMosDownPt.x + absorbInfo.offset.x;
            }
            if (absorbInfo.yabsorb) {
                offsetPt.y = absorbInfo.offset.y;
                tPoint.y = preLMosDownPt.y + absorbInfo.offset.y;
            }
        }
        nModifyInfo = this.modifyCurentGraph(dataManger, offsetPt, mosEvnt);
        if (pCurentGraph){
            dataManger.recordRepaintScope(pCurentGraph);
        }
    }
    return nModifyInfo;
}

LMosDragTool.startDragGraph = function(dataManger,mousePoint){
    let scaling = dataManger.getScaling();
    let mapscale = dataManger.getMapScale();
    let destDragGraph = null;
    let destSelKind = 0;
    let preSelGraph = dataManger.getPreSelGraph();
    let pCurentGraph = dataManger.getCurentGraph();
    if (preSelGraph && pCurentGraph && pCurentGraph != preSelGraph){
        let tempPoint = new LXCOM.LGXPoint();
        tempPoint.clone(mousePoint);
        let preSelKind = preSelGraph.calculateSelKind(tempPoint, scaling, mapscale, false);

        let tempPoint2 = new LXCOM.LGXPoint();
        tempPoint2.clone(mousePoint);
        let curentSelKind = pCurentGraph.calculateSelKind(tempPoint2, scaling, mapscale, false);
        if (preSelKind > 0 && curentSelKind > 0){
            destDragGraph = preSelGraph;
            destSelKind = preSelKind;
        }
        else if (preSelKind == 0 && curentSelKind > 0){
            destDragGraph = pCurentGraph;
            destSelKind = curentSelKind;
        }
        else if (preSelKind > 0 && curentSelKind == 0){
            destDragGraph = preSelGraph;
            destSelKind = preSelKind;
        }
    }
    else if (pCurentGraph){
        destDragGraph = pCurentGraph;
        let tempPoint = new LXCOM.LGXPoint();
        tempPoint.clone(mousePoint);
        destSelKind = pCurentGraph.calculateSelKind(tempPoint, scaling, mapscale, false);
    }
    
    if (!destDragGraph || (destDragGraph && destDragGraph.getLock()) || destSelKind < 1){
        return false;
    }

    dataManger.setCurentGraph(destDragGraph);
    destDragGraph.setSelect(1);
    destDragGraph.setSelKind(destSelKind);
    destDragGraph.setModifing(1);
    dataManger.m_pOperStepService.record(1, [destDragGraph]);
    return true;
}



LMosDragTool.modifyCurentGraph = function (dataManger, offsetPt, mosEvnt) {
    var pCurentGraph = dataManger.getCurentGraph();
    if (pCurentGraph == null) {
        return 0;
    }

    if (pCurentGraph.getSelKind() == 0) {
        return 0;
    }

    var shiftdown = mosEvnt.shiftKey;
    var ctrlKeyDown = mosEvnt.ctrlKey;
    this.modifyGraphByOffset(dataManger, pCurentGraph, offsetPt, false, shiftdown, ctrlKeyDown);
    return 1;
}

LMosDragTool.modifyGraphByOffset = function (dataManger, pGraph, offsetPt, shiftdown, ctrldown) {
    var cmdinfo = new LXCOM.LXModifyCMDInfo();
    cmdinfo.mapscale = dataManger.getMapScale();
    cmdinfo.scaling = dataManger.getScaling();
    cmdinfo.ctrldown = ctrldown;
    cmdinfo.shiftdown = shiftdown;
    dataManger.recordRepaintScope(pGraph);
    var failed = pGraph.modifyByOffset(offsetPt, cmdinfo);
    dataManger.recordRepaintScope(pGraph);
    return failed;
}



export default { LMosDragTool }